//
//  NSString+TimeStamp.m
//  MoodDiary
//
//  Created by MoodDiary on 2020/1/16.
//  Copyright © 2020 MoodDiary. All rights reserved.
//

#import "NSString+TimeStamp.h"

static NSString * const kDateFormat = @"yyyyMMddHHmmss";

static NSString * const kDateFormat2 = @"yyyy/MM/dd HH:mm";

static NSString * const kDateFormat3 = @"HH";

static NSString * const kDateFormat4 = @"yyyy年MM月dd日 HH:mm:ss";

static NSString * const kDateFormat5 = @"mm";

static NSString * const kDateFormat6 = @"yyyy年MM月dd日";

static NSString * const kDateFormat7 = @"MM";

static NSString * const kDateFormat8 = @"yyyy-MM";

static NSString * const kDateFormat9 = @"yyyy";

static NSString * const kDateFormat10 = @"dd";

static NSString * const kDateFormat11 = @"yyyy-MM-dd";

static NSString * const kDateFormat12 = @"HH:mm";

static NSString * const kDateFormat13 = @"MM月dd日";

static NSString * const kDateFormat14 = @"yyyy-MM-dd HH:mm";

static NSString * const kDateFormat15 = @"ss";

@implementation NSString (TimeStamp)

+ (NSString *)getMonthAndDay:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat13];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (NSString *)getHourAndMinute:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat12];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (NSString *)getYear:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat9];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (NSString *)getMonth:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat7];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (NSString *)getDay:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat10];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (BOOL)isThisYear:(NSString *)timeStamp {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat9];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    
    NSString *currentYear = [self currentYear];
    if ([dateString isEqualToString:currentYear]) {
        return YES;
    }
    return NO;
}

+ (BOOL)isThisMonth:(NSString *)timeStamp {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat7];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    
    NSString *currentMonth = [self currentMonth];
    if ([dateString isEqualToString:currentMonth]) {
        return YES;
    }
    return NO;
}

+ (NSString *)convertTimeStringToTimeStamp:(NSString *)timeString withFormart:(NSString *)formart{
    
    if (!formart.length) {
        return nil;
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formart.length?formart:formart];
    NSDate *date = [formatter dateFromString:timeString];
    NSTimeInterval timeInterval = [date timeIntervalSince1970];
    return [NSString stringWithFormat:@"%.0f", timeInterval];
}

+ (NSString *)convertTimeStringToTimeStamp:(NSString *)timeString{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:kDateFormat14];
    NSDate *date = [formatter dateFromString:timeString];
    NSTimeInterval timeInterval = [date timeIntervalSince1970];
    return [NSString stringWithFormat:@"%.0f", timeInterval];
}

+ (NSString *)convertTimeStringToTimeStamp2:(NSString *)timeString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:kDateFormat4];
    NSDate *date = [formatter dateFromString:timeString];
    NSTimeInterval timeInterval = [date timeIntervalSince1970];
    return [NSString stringWithFormat:@"%.0f", timeInterval];
}

+ (NSString *)convertTimeStampToMH:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    //如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //long long time = [timeStr longLongValue] / 1000;
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat6];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (NSString *)convertTimeStampToTimeString:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    //如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //long long time = [timeStr longLongValue] / 1000;
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (NSString *)convertTimeStampToTimeString2:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    //如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //long long time = [timeStr longLongValue] / 1000;
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat2];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (NSString *)compareWithCurrrentDate:(NSString *)timeStamp {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self currentTimeStamp] doubleValue]];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue]];
    NSTimeInterval seconds = [date2 timeIntervalSinceDate:date];
    NSInteger days = seconds / 60 / 60 / 24;
    return [NSString stringWithFormat:@"%ld",days];
}

+(NSString *)compareWithDate1:(NSString *)timeStamp1 toDate2:(NSString *)timeStamp2{
    
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:[timeStamp1 doubleValue]];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:[timeStamp2 doubleValue]];
    NSTimeInterval seconds = [date2 timeIntervalSinceDate:date1];
    NSInteger days = seconds / 60 / 60 / 24;
    return [NSString stringWithFormat:@"%ld",days];
    
}

+(NSString *)compareDayDiffWithDate1:(NSString *)timeStamp1 toDate2:(NSString *)timeStamp2{
    
    NSString *timeStr1 = [self convertTimeStampToMH:timeStamp1];
    NSString *timeStr2 = [self convertTimeStampToMH:timeStamp2];
    
    NSString *tempTimeStr1 = [self convertTimeStringToTimeStamp:timeStr1 withFormart:kDateFormat6];
    NSString *tempTimeStr2 = [self convertTimeStringToTimeStamp:timeStr2 withFormart:kDateFormat6];
    
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:[tempTimeStr1 doubleValue]];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:[tempTimeStr2 doubleValue]];
    NSTimeInterval seconds = [date2 timeIntervalSinceDate:date1];
    NSInteger days = seconds / 60 / 60 / 24;
    return [NSString stringWithFormat:@"%ld",days];
    
}

+ (NSString *)compareWithCurrrentDate2:(NSString *)timeStamp {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self currentTimeStamp] doubleValue]];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue]];
    NSTimeInterval seconds = [date2 timeIntervalSinceDate:date];
    return [NSString stringWithFormat:@"%.0f",seconds];
}

+ (NSString *)compareWithCurrrentDate3:(NSString *)timeStamp {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self currentTimeStamp] doubleValue]];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue]];
    NSTimeInterval seconds = [date timeIntervalSinceDate:date2];
    return [NSString stringWithFormat:@"%.0f",seconds];
}

//获取当前时间
+ (NSString *)currentTime {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

+ (NSString *)currentTime2 {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat2];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

+ (NSString *)currentHour {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat3];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

+ (NSString *)currentMinute {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat5];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

+ (NSString *)currentSecond {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat15];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

+ (NSString *)currentMonthAndDay {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat6];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

+ (NSString *)currentMonth {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat7];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

+ (NSString *)monthAndDay:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat6];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (NSString *)currentYearAndMonth {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat8];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

+ (NSString *)currentYear {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat9];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

+ (NSString *)currentDay {
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:kDateFormat10];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}

+ (NSString *)currentTimeStamp {
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time = [date timeIntervalSince1970];// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    return timeString;
}

+ (NSString *)covertDateToStringWith:(NSDate *)date {
    NSTimeInterval time = [date timeIntervalSince1970];// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    return timeString;
}

+ (NSString *)convertTimeStampToTimeString3:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    //如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //long long time = [timeStr longLongValue] / 1000;
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat4];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (NSString *)convertTimeStampToTimeString4:(NSString *)timeStamp {
    long long time = [timeStamp longLongValue];
    //如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //long long time = [timeStr longLongValue] / 1000;
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:kDateFormat14];
    NSString *timeString=[formatter stringFromDate:date];
    return timeString;
}

+ (NSString *)leftTimeWithSeconds:(NSInteger)second {
    
    NSString *returnStr = @"";
    
    //秒 不到分钟
    if (second < 60) {
        return [NSString stringWithFormat:@"00:%02ld", second];
    }
    
    //分钟 不到小时
    if (second >= 60 && second < 60 * 60) {
        NSInteger minute = second / 60;
        second = second % 60;
        return [NSString stringWithFormat:@"%02ld:%02ld", minute, second];
    }
    
    //小时
    if (second >= 60 * 60) {
        NSInteger hour = second / 60 / 60;
        
        NSInteger leftSecond = second - hour * 60 * 60;
        
        NSInteger minute = leftSecond / 60;
        second = leftSecond % 60;
        return [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hour, minute, second];
    }
    
    return returnStr;
}

+ (NSString *)leftTimeWithSeconds2:(NSInteger)second {
    NSString *returnStr = @"";
      
      //秒 不到分钟
      if (second < 60) {
          return [NSString stringWithFormat:@" 00:00:%02ld", second];
      }
      
      //分钟 不到小时
      if (second >= 60 && second < 60 * 60) {
          NSInteger minute = second / 60;
          second = second % 60;
          return [NSString stringWithFormat:@" 00:%02ld:%02ld", minute, second];
      }
      
      //小时
      if (second >= 60 * 60) {
          NSInteger hour = second / 60 / 60;
          
          NSInteger leftSecond = second - hour * 60 * 60;
          
          NSInteger minute = leftSecond / 60;
          second = leftSecond % 60;
          return [NSString stringWithFormat:@" %02ld:%02ld:%02ld", hour, minute, second];
      }
    
    return returnStr;
}

+ (NSString *)getChineseHourDescription {
    NSInteger currentHour = [self currentHour].integerValue;
    if (currentHour < 8) {
        return @"早上";
    }
    if (currentHour < 11) {
        return @"上午";
    }
    if (currentHour < 13) {
        return @"中午";
    }
    if (currentHour < 18) {
        return @"下午";
    }
    return @"晚上";
}

+ (NSString *)dateWeekWithTimeStamp:(NSString *)timeStamp {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp.integerValue];

    NSArray *weekdays = @[@"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六"];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:date];
        
    return weekdays[theComponents.weekday - 1];
}

@end
